package com.itheima.springboottlias.controller;

import com.itheima.springboottlias.common.utils.JwtUtils;
import com.itheima.springboottlias.pojo.Emp;
import com.itheima.springboottlias.service.EmpService;
import com.itheima.springboottlias.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * @author Taotao
 * @date 2023/7/16 17:10
 * @desciption: 登录控制器
 */
@RestController
public class LoginController {
    @Autowired
    private EmpService empService;

    @PostMapping("/login")
    public Result login(@RequestBody Emp emp) {
        Emp empExits = empService.login(emp.getUsername(), emp.getPassword());
        if (empExits == null) {
            return Result.error("登陆失败");
        }
        HashMap<String, Object> params = new HashMap<>();
        params.put("userId", empExits.getId());
        String useId = JwtUtils.generateJwt(params);

        return Result.success(useId);
    }
}
